package com.zhangwei.touch;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * 第一步实例化GestrueDetector
 * 第二步实例化手势监听 OnGestureListener
 * 第三步重新onTouchevent
 * public boolean onTouchEvent(MotionEvent event) {
 * return gestureDetector.onTouchEvent(event);
 * }
 * 第四步 根据业务需求处理手势
 */
public class GestrueActivity extends AppCompatActivity {
    private static final String TAG = "GestrueActivity";
    /**
     * 第一步实例化的GestureDetector
     */
    private GestureDetector gestureDetector;
    private ImageView img;
    private Bitmap[] bitmaps = new Bitmap[7];
    private int index = 1;
        
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gestrue);
        initData();
        initView();
        gestureDetector = new GestureDetector(this, new MySimpleOnGestureListener());
    }

    /**
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private void initData() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        bitmaps[0] = BitmapFactory.decodeResource(getResources(), R.mipmap.a1, options);
        bitmaps[1] = BitmapFactory.decodeResource(getResources(), R.mipmap.a2, options);
        bitmaps[2] = BitmapFactory.decodeResource(getResources(), R.mipmap.a3, options);
        bitmaps[3] = BitmapFactory.decodeResource(getResources(), R.mipmap.a4, options);
        bitmaps[4] = BitmapFactory.decodeResource(getResources(), R.mipmap.a5, options);
        bitmaps[5] = BitmapFactory.decodeResource(getResources(), R.mipmap.a6, options);
        bitmaps[6] = BitmapFactory.decodeResource(getResources(), R.mipmap.a7, options);

    }


    private void initView() {
        img = (ImageView) findViewById(R.id.grally);
        img.setImageBitmap(bitmaps[0]);
    }

    public class MySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            int x1 = (int) e1.getX();
            int x2 = (int) e2.getX();
            int y1 = (int) e1.getY();
            int y2 = (int) e2.getY();

            if (Math.abs(x1 - x2) > Math.abs(y1 - y2)) {
                if (x1 - x2 > 0) {
                    //从右向左滑动
                    if (index < 7) {
                        index++;
                        img.setImageBitmap(bitmaps[index - 1]);
                    }
                } else {
                    //从左至右滑动
                    if (index > 1) {
                        index--;
                        img.setImageBitmap(bitmaps[index - 1]);
                    }
                }
            }

            return true;
        }
    }


}
